/**
 * Created by Administrator on 2017/1/18 0018.
 */
var pageSize = 5;//每页条数
var pageStart = 1;//当前页码
var pageCount = 10;//总页数
islogin = 1;
var type = 0;

$(function () {
    $(".fixed-bottom a").eq(0).click(function () {
        window.location.href = "/door/front_end/weChat/student/card_weChat_student.html";
    });
    $(".fixed-bottom a").eq(1).click(function () {
        window.location.href = "/door/front_end/weChat/student/apply.record_weChat_student.html";
    });
    init();
});

function init() {
    $.ajax({
        url: "/door/StudentFront/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (checkReturn(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    $("a[href='#out']").click(function () {
        type = 0;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-out-start").focusout(function () {
        type = 0;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-out-end").focusout(function () {
        type = 0;
        pageStart = 1;
        getCardRecords();
    });
    $("a[href='#in']").click(function () {
        type = 1;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-in-start").focusout(function () {
        type = 1;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-in-end").focusout(function () {
        type = 1;
        pageStart = 1;
        getCardRecords();
    });
    $("a[href='#unusual']").click(function () {
        type = 2;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-unusual-start").focusout(function () {
        type = 2;
        pageStart = 1;
        getCardRecords();
    });
    $("#date-unusual-end").focusout(function () {
        type = 2;
        pageStart = 1;
        getCardRecords();
    });
    getCardRecords();

}

function getCardRecords() {
    var sdate = null;
    var edate = null;
    if (type == 0) {
        sdate = $("#date-out-start").val();
        edate = $("#date-out-end").val();
    } else if (type == 1) {
        sdate = $("#date-in-start").val();
        edate = $("#date-in-end").val();
    } else {
        sdate = $("#date-unusual-start").val();
        edate = $("#date-unusual-end").val();
    }
    if (sdate == "") {
        sdate = "1970-01-01 00:00:00";
    }
    if (edate == "") {
        edate = "2160-01-01 00:00:00";
    }
    var startDate = new Date(sdate.replace("T"," "));
    var endDate = new Date(edate.replace("T"," "));

    $.ajax({
        url: "/door/StudentFront/getCardRecords?" + new Date().getTime(),
        type: "post",
        data: {
            startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
            endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
            pageSize: pageSize,
            pageStart: pageStart,
            type: type
        },
        async: false,
        success: function (str) {
            var table = null;
            switch (type) {
                case 0:
                    table = $("#out table");
                    break;
                case 1:
                    table = $("#in table");
                    break;
                case 2:
                    table = $("#unusual table");
                    break;
            }
            table.empty();
            if (checkReturn(str)) {
                if (str.length == 0) {
                    layer.msg("没有数据", {time: 1000});
                }

                for (var i = 0; i < str.length; i++) {
                    if (type != 2) {
                        table.append("<tr><td>" + str[i].studentName + "</td><td>" + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
                    } else {
                        table.append("<tr><td>" + str[i].studentName + "</td><td>" + (str[i].inoutType == 1 ? '出校' : '进校') + "</td><td>"
                            + DateUtils.format(new Date(str[i].createTime), 'yyyy-MM-dd HH:mm:ss') + "</td></tr>");
                    }
                }
            }
        }
    });
    $.ajax({
        url: "/door/StudentFront/getCardRecords?" + new Date().getTime(),
        type: "post",
        data: {
            startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
            endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
            type: type
        },
        async: false,
        success: function (str) {
            if (str != 0) {
                pageCount = Math.floor((str + pageSize - 1) / pageSize);
                setTableBTN();
            } else {
                layer.msg("没有数据", {time: 1000});
            }
        }
    });
}

function checkReturn(str) {
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    // window.location.href = "/door/";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function setTableBTN() {
    $("#page").unbind();

    $("#page option").remove();

    for(var i =1;i<=pageCount;i++){
        if(i!=pageStart)
            $("#page").append("<option>"+i+"</option>");
        else
            $("#page").append("<option selected=true>"+i+"</option>");
    }

    $("#page").change(function (i) {
        console.log($("#page").find("option:selected").text());
        pageStart = parseInt($("#page").find("option:selected").text());
        getCardRecords();
    });

    $(".page-nav span").each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
                if (pageStart > 1) {
                    pageStart--;
                    getCardRecords();
                } else {
                    layer.msg("当前是第一页", {time: 1000});
                }
            };
        } else if (i == 2) {//下一页按钮
            this.onclick = function () {
                if (pageStart < pageCount) {
                    pageStart++;
                    getCardRecords();
                } else {
                    layer.msg("当前是最后一页", {time: 1000});
                }
            };
        } else if (i == 0) {
            this.onclick = function () {
                pageStart = 1;
                getCardRecords();
            };
        } else if (i == 3) {
            this.onclick = function () {
                pageStart = pageCount;
                getCardRecords();
            };
        }
    });
}